package com.sydx.controller;

import com.sydx.entity.Vip;
import com.sydx.service.VipService;
import com.sydx.utils.MapControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
// 班级管理
@Controller
@RequestMapping("/vip")
public class VipController {

    private static final String LIST = "vip/list";
    private static final String ADD = "vip/add";
    private static final String UPDATE = "vip/update";

    @Autowired
    private VipService vipService;

    @GetMapping("/add") // 跳转新增页面
    public String create(){
        return ADD;
    }

    @PostMapping("/create") // 写入数据
    @ResponseBody
    public Map<String,Object> create(@RequestBody Vip vip){
        int result = vipService.create(vip);
        if(result<=0){
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public Map<String,Object> delete(@PathVariable("id") Integer id){
        int result = vipService.delete(id);
        if(result<=0){
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @PostMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(String ids){
        int result = vipService.delete(ids);
        if(result<=0){
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @PostMapping("/update")
    @ResponseBody
    public Map<String,Object> update(@RequestBody Vip vip){
        int result = vipService.update(vip);
        if(result<=0){
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap modelMap){
        Vip vip = vipService.detail(id);
        modelMap.addAttribute("vip",vip);
        return UPDATE;
    }

    @PostMapping("/query")
    @ResponseBody
    public Map<String,Object> query(@RequestBody Vip vip){
        List<Vip> list = vipService.query(vip);
        // 分页
        Integer count = vipService.count(vip);
        return MapControl.getInstance().success().page(list,count).getMap();
    }

    @GetMapping("/list")
    public String list(){
        return LIST;
    }

    }
